home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 001-100 / 001-025 / 010 / trek73 / src / cmds2.c < prev    next >
C/C++ Source or Header  |  1995-03-17  |  10KB  |  463 lines

  1. /*
  2.  * TREK73: cmds2.c
  3.  *
  4.  * User Commands
  5.  *
  6.  * pos_report, pos_display, pursue, helm, self_scan, scan
  7.  *
  8.  * (print_damages)
  9.  *
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include "defines.h"
  14. #include "structs.h"
  15.  
  16. #ifndef AMIGA
  17. #ifdef BSD
  18. #include <strings.h>
  19. #else
  20. #include <string.h>
  21. #endif
  22. #endif
  23.  
  24. extern char title[];
  25. extern char science[];
  26. extern char engineer[];
  27. extern char helmsman[];
  28. extern char nav[];
  29.  
  30.  
  31. pos_report(sp)
  32. struct ship *sp;
  33. {
  34.     extern    int rangefind();
  35.     extern    int bearing();
  36.     extern    struct ship *shiplist[];
  37.     extern    struct list head, *tail;
  38.     struct    ship *sp1;
  39.     struct    torpedo *tp;
  40.     struct    list *lp;
  41.     int    x, y;
  42.     int    range;
  43.     int    bear;
  44.     float    speed;
  45.     int    course;
  46.     int    maxlen = 0;
  47.     char    whitespace[5], who[80];
  48.  
  49.     /*
  50.      * Go through tht list of objects and find out the longest
  51.      * name of any of them.  This is to insure that no matter
  52.      * what the names of the ship, the position report will
  53.      * come out formatted.
  54.      */
  55.     for (lp = &head; lp != tail; lp = lp->fwd) {
  56.         if (lp->type == 0)
  57.             continue;
  58.         if (lp->type == I_SHIP) {
  59.             sp1 = lp->data.sp;
  60.             strcpy(who, sp1->name);
  61.             maxlen = max(maxlen, strlen(who));
  62.         } else {
  63.             tp = lp->data.tp;
  64.             strcpy(who, tp->from->name);
  65.             switch (lp->type) {
  66.                 case I_SHIP:
  67.                 case I_TORPEDO: 
  68.                     continue;
  69.                 case I_PROBE:
  70.                     maxlen = max(maxlen, strlen(who) + 9);
  71.                 case I_ENG:
  72.                     maxlen = max(maxlen, strlen(who) + 12);
  73.                     break;
  74.                 default:
  75.                     break;
  76.             }
  77.         }
  78.     }
  79.     /*
  80.      * Construct a string %ns where n is the length of the
  81.      * longest name.
  82.      */
  83.     sprintf(whitespace, "%%%ds", maxlen);
  84.     /*
  85.      * And print out the position report
  86.      */
  87.     printf(whitespace, " ");
  88.     printf(" warp course bearing range\n");
  89.     for (lp = &head; lp != tail; lp = lp->fwd) {
  90.         if (lp->type == 0)
  91.             continue;
  92.         sp1 = NULL;
  93.         tp = NULL;
  94.         if (lp->type == I_SHIP) {
  95.             sp1 = lp->data.sp;
  96.             if (sp1->status & S_DEAD)
  97.                 continue;
  98.             x = sp1->x;
  99.             y = sp1->y;
  100.             speed = sp1->warp;
  101.             course = sp1->course;
  102.             strcpy(who, sp1->name);
  103.         } else {
  104.             tp = lp->data.tp;
  105.             if (lp->type == I_TORPEDO)
  106.                 continue;
  107.             x = tp->x;
  108.             y = tp->y;
  109.             speed = tp->speed;
  110.             course = tp->course;
  111.             strcpy(who, tp->from->name);
  112.             switch(lp->type) {
  113.                 case I_PROBE:
  114.                     sprintf(who, "%s probe %d", who, tp->id);
  115.                     break;
  116.                 case I_ENG:
  117.                     strcat(who, " engineering");
  118.                     break;
  119.                 default:
  120.                     break;
  121.             }
  122.         }
  123.         bear = bearing(sp->x, x, sp->y, y);
  124.         range = rangefind(sp->x, x, sp->y, y);
  125.         printf(whitespace, who);
  126.         printf(" %4.1f   %3d   ", speed, course);
  127.         if ((sp1 != NULL) && (sp1 == shiplist[0])) {
  128.             if (shiplist[0]->target != NULL)
  129.                 if (shiplist[0]->eluding)
  130.                     printf("eluding %s\n", shiplist[0]->target->name);
  131.                 else
  132.                     printf("pursuing %s\n", shiplist[0]->target->name);
  133.             else
  134.                 printf("           \n");
  135.         } else
  136.             printf("%3d  %5d\n",  bear, range);
  137.     }
  138. }
  139.  
  140.  
  141. pos_display(sp)
  142. struct ship *sp;
  143. {
  144.     extern    struct list head, *tail;
  145.     register int i;
  146.     register int j;
  147.     int    range;
  148.     char    buf1[20];
  149.     int    x, y;
  150.     float    xf, yf;
  151.     int    h, v;
  152.     struct    list *lp;
  153.     struct    ship *sp1;
  154.     struct    torpedo *tp;
  155.     char    map[13][23];
  156.     char    c;
  157.  
  158.     if (sp->status & S_SENSOR) {
  159.         printf("%s: sensors are damaged.\n", science);
  160.         return 0;
  161.     }
  162.     printf("   display to [100+] ");
  163.     if (Gets(buf1) == NULL)
  164.         return 0;
  165.     range = atoi(buf1);
  166.     if (range < 100 || range > 10000)
  167.         return 0;
  168.     /*
  169.      * Compensation for aspect ratio of the output device
  170.      */
  171.     x = range/10;
  172.     y = range/6;
  173.     for (i=0; i<13; i++) {
  174.         if (i == 0 || i == 12)
  175.             strcpy(map[i], "---------------------");
  176.         else
  177.             strcpy(map[i], "                     ");
  178.     }
  179.     map[6][10] = '+';
  180.     for (lp = &head; lp != tail; lp = lp->fwd) {
  181.         if (lp->data.sp == sp)
  182.             continue;
  183.         if (lp->type == I_SHIP) {
  184.             sp1 = lp->data.sp;
  185.             xf = sp1->x - sp->x;
  186.             yf = sp1->y - sp->y;
  187.         } else {
  188.             tp = lp->data.tp;
  189.             xf = tp->x - sp->x;
  190.             yf = tp->y - sp->y;
  191.         }
  192.         v = yf/y + 6.5;
  193.         h = xf/x + 10.5;
  194.         if (v < 0 || v > 12)
  195.             continue;
  196.         if (h < 0 || h > 20)
  197.             continue;
  198.         switch (lp->type) {
  199.             case I_SHIP:
  200.                 c = lp->data.sp->name[0];
  201.                 break;
  202.             case I_TORPEDO:
  203.                 c = ':';
  204.                 break;
  205.             case I_ENG:
  206.                 c = '#';
  207.                 break;
  208.             case I_PROBE:
  209.                 c = '*';
  210.                 break;
  211.             default:
  212.                 c = '?';
  213.                 break;
  214.         }
  215.         map[12 - v][h] = c;
  216.     }
  217.     for (i=0; i<13; i++) {
  218.         for (j=0; j<21; j++)
  219.             if (map[i][j] != ' ')
  220.                 break;
  221.         if (j != 21)
  222.             printf("%s", map[i]);
  223.         printf("\n");
  224.     }
  225.     return 0;
  226. }
  227.  
  228.  
  229. pursue(sp)
  230. struct ship *sp;
  231. {
  232.     extern    struct ship *ship_name();
  233.     extern    double atof();
  234.     register int i;
  235.     char    buf1[20];
  236.     struct    ship *ep;
  237.     float    warp;
  238.  
  239.     if (sp->status & S_COMP) {
  240.         printf("%s: Impossible, %s, our computer is dead\n",science ,title);
  241.         return 0;
  242.     }
  243.     printf("   Mr. %s, pursue [who] ", nav);
  244.     if (Gets(buf1) == NULL)
  245.         return 0;
  246.     ep = ship_name(buf1,ENEMYONLY);
  247.     if (ep == NULL)
  248.         return 0;
  249.     printf("   Mr. %s, warp factor [-9 to 9] ", helmsman);
  250.     if (Gets(buf1) == NULL)
  251.         return 0;
  252.     warp = (float) atof(buf1);
  253.     if (warp > 9.0 || warp < -9.0) {
  254.         printf("%s: %s, the engines canna go that fast!\n",engineer, title);
  255.         return 0;
  256.     }
  257.     if ((warp > 1.0 || warp < -1.0) && (sp->status & S_WARP)) {
  258.         printf("%s: Warp drive is dead, Captain.\n", science);
  259.         warp = warp < 0.0 ? -1.0 : 1.0;
  260.     }
  261.     sp->newwarp = warp;
  262.     sp->target = ep;
  263.     /* Set eluding flag to 0, which means pursue */
  264.     sp->eluding = 0;
  265.     i = bearing(sp->x, ep->x, sp->y, ep->y);
  266.     printf("%s: Aye, %s, coming to course %d.\n", nav, title, i);
  267.     sp->newcourse = i;
  268.     return 1;
  269. }
  270.  
  271.  
  272. elude(sp)
  273. struct ship *sp;
  274. {
  275.     extern    struct ship *ship_name();
  276.     extern    double atof();
  277.     register int i;
  278.     char    buf1[20];
  279.     struct    ship *ep;
  280.     float    warp;
  281.  
  282.     if (sp->status & S_COMP) {
  283.         printf("%s: impossible, %s, our computer is dead\n",science, title);
  284.         return 0;
  285.     }
  286.     printf("   Mr. %s, elude [who] ", nav);
  287.     if ((Gets(buf1) == NULL) || (buf1 == NULL))
  288.         return 0;
  289.     ep = ship_name(buf1,ENEMYONLY);
  290.     if (ep == NULL)
  291.         return 0;
  292.     printf("   Mr. %s, warp factor [-9 to 9] ", helmsman);
  293.     if (Gets(buf1) == NULL)
  294.         return 0;
  295.     warp = (float) atof(buf1);
  296.     if (warp > 9.0 || warp < -9.0) {
  297.         printf("%s: %s, the engines canna go that fast!\n",engineer, title);
  298.         return 0;
  299.     }
  300.     if ((warp > 1.0 || warp < -1.0) && (sp->status & S_WARP)) {
  301.         printf("%s: Warp drive is dead, Captain.\n", science);
  302.         warp = warp < 0.0 ? -1.0 : 1.0;
  303.     }
  304.     sp->newwarp = warp;
  305.     sp->target = ep;
  306.     /* Set eluding flag to 1, which means elude */
  307.     sp->eluding = 1;
  308.     i = bearing(sp->x, ep->x, sp->y, ep->y);
  309.     i = rectify(i + 180);
  310.     printf("%s: Aye, %s, coming to course %d.\n", nav, title, i);
  311.     sp->newcourse = i;
  312.     return 1;
  313. }
  314.  
  315. helm(sp)
  316. struct ship *sp;
  317. {
  318.     extern    double atof();
  319.     char    buf1[20];
  320.     register int course;
  321.     float    warp;
  322.  
  323.     printf("   Mr. %s, come to course [0-359] ", nav);
  324.     if (Gets(buf1) == NULL)
  325.         return 0;
  326.     course = atoi(buf1);
  327.     if (course < 0 || course > 360)
  328.         return 0;
  329.     printf("   Mr. %s, warp factor [-9 to 9] ", helmsman);
  330.     if (Gets(buf1) == NULL)
  331.         return 0;
  332.     warp = (float) atof(buf1);
  333.     if (warp > 9.0 || warp < -9.0) {
  334.         printf("%s: %s, the engines canna go that fast!\n",engineer, title);
  335.         return 0;
  336.     }
  337.     if ((warp > 1.0 || warp < -1.0) && (sp->status & S_WARP)) {
  338.         printf("%s: Warp drive is dead, Captain.\n", science);
  339.         warp = warp < 0.0 ? -1.0 : 1.0;
  340.     }
  341.     sp->newwarp = warp;
  342.     sp->newcourse = course;
  343.     sp->target = NULL;
  344.     sp->eluding = 0;
  345.     printf("%s: aye, %s.\n", nav, title);
  346.     return 1;
  347. }
  348.  
  349. self_scan(sp)
  350. struct ship *sp;
  351. {
  352.     struct ship *ep;
  353.     extern struct ship *shiplist[];
  354.  
  355.     ep = shiplist[0];            /* Scanning ourself */
  356.     print_damage(ep);
  357.     sp = sp;                /* LINT */
  358.     return 1;
  359. }
  360.  
  361. scan(sp)
  362. struct ship *sp;
  363. {
  364.     struct    ship *ep;
  365.     char    buf1[20];
  366.  
  367.     printf("   %s, scan [who] ", science);
  368.     if (Gets(buf1) == NULL)
  369.         return 0;
  370.     ep = ship_name(buf1,ENEMYONLY);
  371.     if (ep == NULL)
  372.         return 0;
  373.     if (sp != ep && (sp->status & S_SENSOR)) {
  374.         printf ("%s: The sensors are damaged, Captain.\n", science);
  375.         return 0;
  376.     }
  377.     if (sp == ep) {
  378.         printf ("%s: Captain, don't you mean 'Damage Report'?\n", science);
  379.         return 0;
  380.     }
  381.     print_damage(ep);
  382.     return 1;
  383. }
  384.  
  385. print_damage(ep)
  386. struct ship *ep;
  387. {    
  388.     extern char *statmsg[];
  389.     register int i;
  390.     register int j;
  391.     register float k;
  392.  
  393.     printf("\n\nDamages to the %s\n", ep->name);
  394.     for (i=0; i<5; i++) {
  395.         if (ep->status & 1<<i)
  396.             printf("%s.\n", statmsg[i]);
  397.     }
  398.     printf("survivors: %d\n", ep->crew);
  399.     printf("\nPhasers Control");
  400.     for (i=0; i<4; i++) {
  401.         if (ep->phasers[i].status & P_DAMAGED)
  402.             printf("\tdamaged");
  403.         else if (ep->phasers[i].target == NULL)
  404.             printf("\tmanual");
  405.         else
  406.             printf("\t%.7s", ep->phasers[i].target->name);
  407.     }
  408.     printf("\n\t turned");
  409.     for (i=0; i<4; i++)
  410.         if (ep->phasers[i].status & P_DAMAGED)
  411.             printf("\t");
  412.         else if (ep->phasers[i].target == NULL)
  413.             printf("\t%d", ep->phasers[i].bearing);
  414.         else
  415.             printf("\tLOCKED");
  416.     printf("\n\t  level");
  417.     for (i=0; i<4; i++)
  418.         if (ep->phasers[i].status & P_DAMAGED)
  419.             printf("\t");
  420.         else
  421.             printf("\t%-2d", ep->phasers[i].load);
  422.     printf("\n");
  423.     printf("\nTubes\tcontrol");
  424.     for (i=0; i<6; i++) {
  425.         if (ep->tubes[i].status & T_DAMAGED)
  426.             printf("\tdamaged");
  427.         else if (ep->tubes[i].target == NULL)
  428.             printf("\tmanual");
  429.         else
  430.             printf("\t%.7s", ep->tubes[i].target->name);
  431.     }
  432.     printf("\n\t turned");
  433.     for (i=0; i<6; i++)
  434.         if (ep->tubes[i].status & T_DAMAGED)
  435.             printf("\t");
  436.         else if (ep->tubes[i].target == NULL)
  437.             printf("\t%d", ep->tubes[i].bearing);
  438.         else
  439.             printf("\tLOCKED");
  440.     printf("\n\t  level");
  441.     for (i=0; i<6; i++)
  442.         if (ep->tubes[i].status & T_DAMAGED)
  443.             printf("\t");
  444.         else
  445.             printf("\t%-2d", ep->tubes[i].load);
  446.     printf("\n");
  447.     printf("\nShields\t levels");
  448.     for (i=0; i<4; i++) {
  449.         j = 100 * ep->shields[i].eff * ep->shields[i].drain;
  450.         printf("\t%-2d", j);
  451.     }
  452.     printf("\n\t drains");
  453.     for (i=0; i<4; i++) {
  454.         k = ep->shields[i].attemp_drain;
  455.         printf("\t%-4.2f", k);
  456.     }
  457.     printf("\n\nefficiency: %3.1f\t\tfuel remaining: %d\n",
  458.         ep->eff, ep->energy);
  459.     printf("regeneration: %4.1f\tfuel capacity: %d\n",
  460.         ep->regen, ep->pods);
  461.     return 1;
  462. }
  463.